package com.toune.dltools.adapter

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.tamsiree.rxkit.view.RxToast

class DLAdapterTool<T> {

    private var cxt: Context? = null
    private var rclView: RecyclerView? = null
    private var mAdapter: BaseQuickAdapter<T, BaseViewHolder>? = null
    private var count:Int = 1

    constructor(context: Context, rcl: RecyclerView, adapter: BaseQuickAdapter<T, BaseViewHolder>) {
        cxt = context
        rclView = rcl
        mAdapter = adapter
        initAdapter()
    }

    constructor(
        context: Context,
        rcl: RecyclerView,
        adapter: BaseQuickAdapter<T, BaseViewHolder>,
        count: Int
    ) {
        rclView = rcl
        mAdapter = adapter
        this.count = count
        initAdapter()
    }


    private fun initAdapter() {
        if (mAdapter == null) {
            RxToast.info("adapter不能为null")
            return
        } else {
            rclView!!.layoutManager = GridLayoutManager(cxt,count)
            rclView!!.adapter = mAdapter
        }
    }

    fun notifyAdapter(){
        if (mAdapter == null) {
            RxToast.info("adapter不能为null")
            return
        } else {
            mAdapter!!.notifyDataSetChanged()
        }
    }

    fun getAdapter(): BaseQuickAdapter<T, BaseViewHolder>? {
        return mAdapter
    }
}